package org.bedracket.pgeg.event;

import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.bedracket.pgeg.PGEGMod;
import org.bedracket.pgeg.init.EModKeyMappings;
import org.bedracket.pgeg.network.OpenTestMenuMessage;
import org.bedracket.pgeg.network.SimpleNetworkHandler;

@Mod.EventBusSubscriber(value = Dist.CLIENT, modid = PGEGMod.MOD_ID)
public class InputEvents {

    @SubscribeEvent(priority = EventPriority.LOWEST)
    public static void onKeyInput(InputEvent.Key event) {
        if (Minecraft.getInstance().screen != null)
            return;

        int key = event.getKey();
        boolean isPressed = !(event.getAction() == 0);
        if (key == EModKeyMappings.OPEN_TEST_MENU.getKey()) {
            SimpleNetworkHandler.PACKET_HANDLER.sendToServer(new OpenTestMenuMessage());
        }
    }

    @SubscribeEvent
    public static void onClientTick(TickEvent.ClientTickEvent event) {
        if (Minecraft.getInstance().screen == null) {
            EModKeyMappings.OPEN_TEST_MENU.getKeyMapping().consumeClick();
        }
    }
}
